/**
 * 打开
 * Select.open()
 * 
    this.select.addEventListener(Select.Click, (e) => {
        this.selectData = e.data
        this.input_3.text = `${this.selectData.province}-${this.selectData.city}-${this.selectData.area}`
    }, this)
    this.input_3.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
        this.select.open()
    }, this)
 * 
 */
class Select extends eui.Component {
    constructor() {
        super()
        this.skinName = 'skins.Select'
    }
    public static Click = 'queren'
    protected createChildren(): void {
        super.createChildren()
        this.init()
    }
    public scroller1: eui.Scroller;
    public select_li1: eui.List;
    public scroller2: eui.Scroller;
    public select_li2: eui.List;
    public scroller3: eui.Scroller;
    public select_li3: eui.List;
    public maks: eui.Rect;

    private city_json;
    private province_json;
    private area_json;
    private itme_h = 66
    public btn: eui.Group;
    public g: eui.Group
    private init() {
        // console.log(this.stage.stageHeight,'init');
        this.g.y = this.stage.stageHeight
        this.province_json = RES.getRes('province_json')
        this.city_json = RES.getRes('city_json')
        this.area_json = RES.getRes('area_json')

        this.select_li1.dataProvider = new eui.ArrayCollection(this.province_json)
        this.select_li2.dataProvider = new eui.ArrayCollection(this.city_json[this.province_json[0].id])
        this.select_li3.dataProvider = new eui.ArrayCollection(this.area_json[this.city_json[this.province_json[0].id][0].id])
        this.select_li1.touchChildren = false
        this.select_li2.touchChildren = false
        this.select_li3.touchChildren = false
        this.visible = false
        this.province = this.province_json[0]
        this.city = this.city_json[this.province_json[0].id][0]
        this.area = this.area_json[this.city_json[this.province_json[0].id][0].id][0]
        this.start()
    }
    private start() {
        this.scroller1.throwSpeed = 0
        this.scroller2.throwSpeed = 0
        this.scroller3.throwSpeed = 0
        // console.log(this.stage.stageHeight,'start');
        this.scroller1.addEventListener(eui.UIEvent.CHANGE_END, this.scroller_1, this)
        this.scroller2.addEventListener(eui.UIEvent.CHANGE_END, this.scroller_2, this)
        this.scroller3.addEventListener(eui.UIEvent.CHANGE_END, this.scroller_3, this)
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.dispatchEventWith(Select.Click, false, {
                'province': this.province.name,
                'city': this.city.name,
                'area': this.area.name
            })
            this.close()
            console.log({
                'province': this.province.name,
                'city': this.city.name,
                'area': this.area.name
            })
        }, this)
        this.maks.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this)
        this.maks.touchEnabled = false
        // this.open()
    }

    public open() {
        this.visible = true
        egret.Tween.get(this.g).to({ y: this.stage.stageHeight - this.g.height }, 400, egret.Ease.quadOut).call(() => {
            this.maks.touchEnabled = true
        })
    }
    public close() {
        egret.Tween.get(this.g).to({ y: this.stage.stageHeight }, 400, egret.Ease.quadOut)
        egret.Tween.get(this).wait(200).set({ visible: false })
    }
    public province = null;
    public city = null;
    public area = null;
    private scroller_1() {
        if (this.scroller1.viewport.scrollV % this.itme_h != 0) {
            let index = (this.scroller1.viewport.scrollV / this.itme_h).toFixed(0)
            this.scroller1.viewport.scrollV = (Number(index)) * this.itme_h
            this.scroller1.$Scroller[9].throwTo((Number(index)) * this.itme_h, 200)
            if (this.province != this.province_json[index]) {
                this.select_li2.dataProvider = new eui.ArrayCollection(this.city_json[this.province_json[index].id])
                this.select_li3.dataProvider = new eui.ArrayCollection(this.area_json[this.city_json[this.province_json[index].id][0].id])
                this.province = this.province_json[index]
                this.city = this.city_json[this.province_json[index].id][0]
                this.area = this.area_json[this.city_json[this.province_json[index].id][0].id][0]
            }
        } else {
            let index = (this.scroller1.viewport.scrollV / this.itme_h).toFixed(0)
            this.scroller1.viewport.scrollV = (Number(index)) * this.itme_h
            this.province = this.province_json[index]
            if (this.province != this.province_json[index]) {
                this.select_li2.dataProvider = new eui.ArrayCollection(this.city_json[this.province_json[index].id])
                this.select_li3.dataProvider = new eui.ArrayCollection(this.area_json[this.city_json[this.province_json[index].id][0].id])
                this.province = this.province_json[index]
                this.city = this.city_json[this.province_json[index].id][0]
                this.area = this.area_json[this.city_json[this.province_json[index].id][0].id][0]
            }
        }
    }
    private scroller_2() {
        if (this.scroller2.viewport.scrollV % this.itme_h != 0) {
            let index1 = (this.scroller1.viewport.scrollV / this.itme_h).toFixed(0)
            let index2 = (this.scroller2.viewport.scrollV / this.itme_h).toFixed(0)
            this.scroller2.viewport.scrollV = (Number(index2)) * this.itme_h
            this.scroller2.$Scroller[9].throwTo((Number(index2)) * this.itme_h, 200)
            if (this.city != this.city_json[this.province_json[index1].id][index2]) {
                this.city = this.city_json[this.province_json[index1].id][index2]
                this.area = this.area_json[this.city.id][0]
                this.select_li3.dataProvider = new eui.ArrayCollection(this.area_json[this.city_json[this.province_json[index1].id][index2].id])
            }
        } else {
            let index1 = (this.scroller1.viewport.scrollV / this.itme_h).toFixed(0)
            let index2 = (this.scroller2.viewport.scrollV / this.itme_h).toFixed(0)
            this.scroller2.viewport.scrollV = (Number(index2)) * this.itme_h
            if (this.city != this.city_json[this.province_json[index1].id][index2]) {
                this.city = this.city_json[this.province_json[index1].id][index2]
                this.area = this.area_json[this.city.id][0]
                this.select_li3.dataProvider = new eui.ArrayCollection(this.area_json[this.city_json[this.province_json[index1].id][index2].id])
            }
        }
    }
    private scroller_3() {
        if (this.scroller3.viewport.scrollV % this.itme_h != 0) {
            let index3 = (this.scroller3.viewport.scrollV / this.itme_h).toFixed(0)
            this.scroller3.viewport.scrollV = (Number(index3)) * this.itme_h
            this.scroller3.$Scroller[9].throwTo((Number(index3)) * this.itme_h, 200)
            this.area = this.area_json[this.city.id][index3]
        } else {
            let index3 = (this.scroller3.viewport.scrollV / this.itme_h).toFixed(0)
            this.area = this.area_json[this.city.id][index3]
        }
    }

}